<?php

namespace App\HttpController\Admin;

use App\Logic\Auth\UserService;
use App\Logic\BackendUserService;
use EasySwoole\HttpAnnotation\AnnotationTag\Api;
use EasySwoole\HttpAnnotation\AnnotationTag\ApiDescription;
use EasySwoole\HttpAnnotation\AnnotationTag\ApiGroup;
use EasySwoole\HttpAnnotation\AnnotationTag\ApiSuccessParam;
use EasySwoole\HttpAnnotation\AnnotationTag\Method;
use EasySwoole\HttpAnnotation\AnnotationTag\Param;

/**
 * @ApiGroup(groupName="用户管理")

 */
class User extends BaseController{

    /**
     * @Api(name="登录",path="/admin/user/login")
     * @ApiGroup(groupName="Supplier")
     * @ApiDescription("编辑供应商")
     * @Param(name="username",description="用户名",required="",type="integer")
     * @Param(name="password",description="密码",required="",type="integer")
     * @ApiSuccessParam(name="username",description="账号")
     * @ApiSuccessParam(name="name",description="用户名")
     * @ApiSuccessParam(name="avatar",description="头像")
     * @ApiSuccessParam(name="type",description="账号类型[1:内部账号 2:商户账号 3:仓库账号]")
     * @ApiSuccessParam(name="menu_list",description="菜单树")
     * @ApiSuccessParam(name="button",description="权限按钮")
     */
    public function login(){
        //判断是否已登陆，已登陆直接输出成功
        $loginForm = new BackendUserService();
        $result = $loginForm->login($this->getJson('username'), $this->getJson('password'));
        return $this->writeJsonByOutput($result);
    }


    /**
     * @Api(name="修改密码",path="/admin/User/password")
     * @ApiGroup(groupName="Supplier")
     * @ApiDescription("修改密码")
     * @Param(name="password",description="密码",required="",type="string")
     */
    public function password(){
        $result = BackendUserService::getInstance()->password($this->getJson('password'));

        return $this->writeJsonByOutput($result);
    }
    /**
     * @Api(name="修改用户密码",path="/api/User/setPwd")
     * @ApiGroup(groupName="Supplier")
     * @ApiDescription("修改密码")
     * @Param(name="username",description="用户名",required="",type="string")
     * @Param(name="password",description="密码",required="",type="string")
     */
    public function setPwd()
    {
        $params = $this->getJson();
        $result = BackendUserService::getInstance()->setPwd($params['username'] ?? '', $params['password'] ?? '');

        return $this->writeJsonByOutput($result);
    }
    /**
     * @Api(name="发送注册短信",path="/api/user/send")
     * @ApiDescription("发送注册短信")
     * @Method(allow={POST})
     * @Param(name="phone",description="手机号码",required="",type="integer")
     */
    public function send() {
        $phone = $this->getJson('phone');

        $result = BackendUserService::getInstance()->sendVerify($phone);

        return $this->writeJsonByOutput($result);
    }

    /**
     * @Api(name="注册用户",path="/api/user/register")
     * @ApiDescription("注册用户")
     * @Method(allow={GET})
     * @Param(name="supplier_name",description="供应商名称",required="",type="string")
     * @Param(name="phone",description="手机号码",required="",type="string")
     * @Param(name="password",description="密码",required="",type="string")
     * @Param(name="code",description="验证码",required="",type="string")
     * @Param(name="authenticity",description="真实性校验 1开启 0关闭",required="",type="string")
     * @Param(name="search_key",description="启信宝真实性校验 返回的查询标识",type="string")
     */
    public function register() {
        $data = $this->getJson();

        $result = BackendUserService::getInstance()->register($data);

        return $this->writeJsonByOutput($result);
    }

    /**
     * @Api(name="商户列表",path="/api/user/merchants")
     * @ApiDescription("商户列表")
     * @Method(allow={GET})
     * @ApiSuccessParam(name="id",description="id")
     * @ApiSuccessParam(name="company_name",description="商户公司名称")
     * @ApiSuccessParam(name="short_name",description="商户简称")
     */
    public function merchants(){
        return $this->writeJson(200, [], '');
        $result = MerchantsService::getInstance()->loginList();

        return $this->writeJsonByOutput($result);
    }

    /**
     * @Api(name="用户日志",path="/api/user/log")
     * @ApiDescription("用户日志")
     * @Method(allow={GET})
     * @Param(name="operator_id",description="操作人ID",type="integer")
     * @Param(name="record_id",description="用户ID",type="integer")
     * @Param(name="start_log_date",description="操作时间开始",type="string")
     * @Param(name="end_log_date",description="操作时间结束",type="string")
     */
    public function log(){
        $params = $this->request()->getQueryParams();
        $result = UserService::getInstance()->logList($params,$this->request());
        return $this->writeJsonByOutput($result);
    }

}
